/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.ana.facade;

import br.gov.ana.entities.Usuario;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author lucas.nunes
 */
@Stateless
public class UsuarioFacade extends AbstractFacade<Usuario> {
    @PersistenceContext(unitName = "ResolucaoConjunta032010PU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UsuarioFacade() {
        super(Usuario.class);
    }
    
    public Usuario logar(Usuario usuario){
        try {
            Query q = em.createQuery("SELECT u FROM Usuario u WHERE u.ureTxLogin = :ureTxLogin AND u.ureTxSenha = :ureTxSenha AND u.ureStatus = 1");
            q.setParameter("ureTxLogin", usuario.getUreTxLogin());
            q.setParameter("ureTxSenha", usuario.getUreTxSenha());            
            return (Usuario) q.getSingleResult();
        } catch (Exception e) {
            return null;
        }
    }
    
    public List<Usuario> findAllUsuarioAtivo (String order){
        try {
            Query q = em.createQuery("SELECT u FROM Usuario u WHERE u.ureStatus = 1 ORDER BY :ureId");
            q.setParameter("ureId", order);
            return (List<Usuario>)q.getResultList();
        } catch (Exception e){
            return null;
        }
    }
    
}
